package com.nebula.service.impl;

import com.nebula.entity.Salary;
import com.nebula.common.DataGridFooter;
import com.nebula.common.PageResult;
import com.nebula.dao.ISalaryDao;
import com.nebula.dao.IUserDao;
import com.nebula.dao.impl.SalaryDaoImpl;
import com.nebula.dao.impl.UserDaoImpl;
import com.nebula.service.ISalaryService;
import com.nebula.util.DateTimeUtil;
import com.nebula.vo.SalaryAnalyzeDGVo;
import com.nebula.vo.SalaryVo;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @author CcccX
 * @create 2019-01-21 14:23
 */
public class SalaryServiceImpl implements ISalaryService {
    private ISalaryDao iSalaryDao = new SalaryDaoImpl();
    private IUserDao iUserDao = new UserDaoImpl();

    @Override
    public boolean checkNavRole(Integer roleId, String navId) {
        return iSalaryDao.checkNavRole(roleId, navId);
    }

    @Override
    public PageResult<SalaryVo> findByUserIdAndPage(String id, int page, int size, boolean isHistory, int... year) {
        String conditions = " where user_id = '" + id + "'";
        if (!isHistory) {
            Calendar calendar = Calendar.getInstance();
            int issueYear = calendar.get(Calendar.YEAR);
            conditions += " and issue_year = " + issueYear;
        }else {
            if (year[0] != -1){
                conditions += " and issue_year = " + year[0];
            }
        }
        List<Salary> salaryList = iSalaryDao.findByPage(page, size, conditions);
        List<SalaryVo> salaryVos = new ArrayList<>();
        BigDecimal bd = new BigDecimal(0);
        for (Salary salary : salaryList) {
            SalaryVo salaryVo = assembleSalaryVo(salary);
            bd = bd.add(salary.getSalary());
            salaryVos.add(salaryVo);
        }
        PageResult<SalaryVo> pageResult = new PageResult<>();
        pageResult.setCurrPage(page);
        pageResult.setList(salaryVos);

        Integer totalCount = iSalaryDao.selectCount("salary", conditions);
        Double dc = Math.ceil(totalCount.doubleValue() / size);
        pageResult.setTotalCount(totalCount);
        pageResult.setPageSize(dc.intValue());
        NumberFormat nf = NumberFormat.getCurrencyInstance();
        pageResult.setCurrTotalSalary(nf.format(bd));

        return pageResult;
    }

    @Override
    public PageResult<SalaryAnalyzeDGVo> findSalaryAnalyzeByUserIdAndPage(String id, int page, int size) {
        List<SalaryAnalyzeDGVo> list = iSalaryDao.findSalaryAnalyzeByUserIdAndPage(id, page, size);
        Integer totalCount = list.size();

        PageResult<SalaryAnalyzeDGVo> pageResult = new PageResult<>();
        pageResult.setCurrPage(page);
        pageResult.setList(list);
        pageResult.setTotalCount(totalCount);
        double dc = Math.ceil(totalCount.doubleValue() / size);
        pageResult.setPageSize((int)dc);

        return pageResult;
    }

    @Override
    public DataGridFooter findTotalSalary(String id, boolean isHistory, Object... conditon) {
        BigDecimal totalSalary;
        if (!isHistory) {
            Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            String conditions = " and issue_year = " + year;
            totalSalary = iSalaryDao.getTotalSalary(id, conditions);
        } else {
        	if (conditon[0] != null && (Integer)conditon[0] != -1) {
        		totalSalary = iSalaryDao.getTotalSalary(id," and issue_year = " + conditon[0]);
			}else {
				totalSalary = iSalaryDao.getTotalSalary(id);
			}
        }
        NumberFormat format = NumberFormat.getCurrencyInstance();
        DataGridFooter dataGridFooter = new DataGridFooter();
        if (totalSalary != null) {
            String s = format.format(totalSalary);
            dataGridFooter.setTotalSalary(s);
        } else {
            dataGridFooter.setTotalSalary(format.format(0));
        }

        return dataGridFooter;
    }

    @Override
    public SalaryVo findByID(String id) {
        Salary salary = iSalaryDao.findById(id);

        return assembleSalaryVo2(salary);
    }

    @Override
    public void save(Salary salaryCls) {
        iSalaryDao.save(salaryCls);
    }

    @Override
    public void update(Salary salaryCls) {
        iSalaryDao.update(salaryCls);
    }

    @Override
    public void delete(int id) {
        iSalaryDao.delete(id);
    }

    private SalaryVo assembleSalaryVo(Salary salary) {
        SalaryVo salaryVo = new SalaryVo();
        salaryVo.setId(salary.getId());
        salaryVo.setIssueDate(DateTimeUtil.dateToStr(salary.getIssueDate(), "yyyy-MM-dd"));
        salaryVo.setCreatedTime(DateTimeUtil.dateToStr(salary.getCreatedTime()));
        salaryVo.setUpdatedTime(DateTimeUtil.dateToStr(salary.getUpdatedTime()));
        salaryVo.setRemark(salary.getRemark());
        salaryVo.setUser(salary.getUserId());
        salaryVo.setYear(salary.getIssueYear());
        salaryVo.setMonth(salary.getIssueMonth());
        salaryVo.setSalaryNum(salary.getSalary());

        NumberFormat nf = NumberFormat.getCurrencyInstance();
        if (salary.getSalary() != null) {
            salaryVo.setSalary(nf.format(salary.getSalary()));
        } else {
            salaryVo.setSalary(nf.format(0));
        }

        return salaryVo;
    }

    private SalaryVo assembleSalaryVo2(Salary salary) {
        SalaryVo salaryVo = assembleSalaryVo(salary);
        salaryVo.setSalary(salary.getSalary().toString());
        return salaryVo;
    }
}
